package com.jielihaofeng.chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jielihaofeng.chat.convert.ChatGroupUserConvert;
import com.jielihaofeng.chat.dao.ChatGroupUserDao;
import com.jielihaofeng.chat.entity.ChatGroupUserEntity;
import com.jielihaofeng.chat.query.ChatGroupUserQuery;
import com.jielihaofeng.chat.service.ChatGroupUserService;
import com.jielihaofeng.chat.vo.ChatGroupUserVO;
import com.jielihaofeng.framework.common.page.PageResult;
import com.jielihaofeng.framework.common.service.impl.BaseServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 群组用户表
 *
 * @author 借力好风 949394048@qq.com
 * @since 1.0.0 2022-07-22
 */
@Service
@AllArgsConstructor
public class ChatGroupUserServiceImpl extends BaseServiceImpl<ChatGroupUserDao, ChatGroupUserEntity> implements ChatGroupUserService {

    @Override
    public PageResult<ChatGroupUserVO> page(ChatGroupUserQuery query) {
        IPage<ChatGroupUserEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(ChatGroupUserConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private QueryWrapper<ChatGroupUserEntity> getWrapper(ChatGroupUserQuery query) {
        QueryWrapper<ChatGroupUserEntity> wrapper = new QueryWrapper<>();

        return wrapper;
    }

    @Transactional
    @Override
    public boolean save(ChatGroupUserVO vo) {
        ChatGroupUserEntity entity = ChatGroupUserConvert.INSTANCE.convert(vo);
        return baseMapper.insert(entity) > 0;
    }

    @Override
    public void update(ChatGroupUserVO vo) {
        ChatGroupUserEntity entity = ChatGroupUserConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

    /**
     * 用户退出群组
     *
     * @param groupId 群组id
     * @param userId  用户id
     */
    @Override
    public void exitGroup(Long groupId, Long userId) {
        baseMapper.delete(new LambdaQueryWrapper<ChatGroupUserEntity>()
                .eq(ChatGroupUserEntity::getGroupId, groupId)
                .eq(ChatGroupUserEntity::getUserId, userId)
        );
    }

    /**
     * 获取用户所在群组id列表
     *
     * @param userId 用户id
     * @return 群组id列表
     */
    @Override
    public List<Long> getGroupIdList(Long userId) {
        List<ChatGroupUserEntity> chatGroupUserEntities = baseMapper.selectList(new LambdaQueryWrapper<ChatGroupUserEntity>()
                .eq(ChatGroupUserEntity::getUserId, userId));
        return chatGroupUserEntities.stream().map(ChatGroupUserEntity::getGroupId).collect(Collectors.toList());
    }

    /**
     * 获取群组所有用户id列表
     *
     * @param groupId 群组id
     * @return 群组所有用户id列表
     */
    @Override
    public Set<Long> getUserIdList(Long groupId) {
        List<ChatGroupUserEntity> chatGroupUserEntities = baseMapper.selectList(new LambdaQueryWrapper<ChatGroupUserEntity>()
                .eq(ChatGroupUserEntity::getGroupId, groupId));
        return chatGroupUserEntities.stream().map(ChatGroupUserEntity::getUserId).collect(Collectors.toSet());
    }
}
